var indexPa=1
var totalSum=0;
var trainTitleName='';
var memberName='';
var addressCode='';
var selectObj=[];
var selectAllId=[];
$(function () {
    "use strict";

        if(orgClass=="org_ebb_county"){
                       $("#addressCodeDiv").hide();
                    }else{
                      fillCounty("520600000000","addressCode");
                        $("#addressCode").change(function(){
                                  addressCode=this.value;
                                   loadProduct();
                             });
                    }

        $('#export').click(function () {
                      exportTrainMemberInfo();
                  });
    loadProduct();

});
//导出培训信息
function exportTrainMemberInfo() {
    window.location.href = "/ebb/train/member/export/?trainTitle=" + trainTitleName
        + "&addressCode=" + addressCode;
}
//http://cloud.vimi8.top/industrial/product/allProductInfo?index=1&size=10&trainTitleName=yy&memberName=%E9%93%9C%E4%BB%81
function loadProduct() {
    $.ajax({
        type: "get",
        url: "/ebb/train/member/?index="+indexPa+"&size=10&trainTitle="+trainTitleName+"&memberName="+memberName+"&addressCode="+addressCode,
       // data: JSON.stringify(params),
        contentType: "application/json",
        success: function (result) {
            if (result.data.length > 0) {
                var totalPage = result.totalPages;
               var totalCount = result.totalCount;
                $("#total-page").text(totalPage);
                 $('#product-count').text(totalCount);
                $("#pagination-bar").show();
                $("#pagination-box").html('<ul id="pagination" class="pagination"></ul>');
                $('#pagination').twbsPagination({
                    totalPages: totalPage,
                    totalCount:totalCount,
                    visiblePages: 5,
                    first: '第一页',
                    prev: '上一页',
                    next: '下一页',
                    last: '最后页',
                    onPageClick: function(event,page) {
                        indexPa=page;

                        $.ajax({
                                type: "get",
                                url: "/ebb/train/member/?index="+indexPa+"&size=10&trainTitle="+trainTitleName+"&memberName="+memberName+"&addressCode="+addressCode,
                             //   data: JSON.stringify(params),
                                contentType: "application/json",
                                success: function (result) {
                                    totalSum = result.totalPages;
                                    var d = result.data;
                                    selectObj=d;
                                    $("input[name='checkboxAll']").prop("checked",false);
                                    selectAllId=[];
                                    //渲染数据
                                    $.templates("#product-list-tmpl").link("#product-list-data", d);
                                    totalPages = result.totalPages;
                                    $('#product-count').text(result.totalCount);
                                    $('#page-total').text(result.totalPages);
                                    $('#page-index').val(page);
                                },
                                error: function () {
                                }
                            });
                    },
                    startPage: indexPa
                });
            } else {
                $("#product-list-data").html("<tr><td colspan='8'><h1 align='center' height='300px'>暂无数据</h1></td></tr>");
                $("#pagination-bar").hide();
            }
        }
    });
}


function selectall(obj) {
    selectAllId=[];
    //获取checked属性
    if ($(obj).prop("checked")) {
        //设置checked属性
        $("input[name='checkboxAll']").prop("checked", true);
        $("input[name='check']").prop("checked", true);
        for(var i=0;i<selectObj.length;i++){
            selectAllId.push(selectObj[i].id);
        }
    } else {
        //设置checked属性
        $("input[name='checkboxAll']").prop("checked",false);
        $("input[name='check']").prop("checked", false);
    }
}

function selectsub(id) {
    //获取checked属性
    if ($("#"+id).prop("checked")) {
        //设置checked属性
        $("input[id=id]").prop("checked", true);
        var count=0;
        for(var i=0;i<selectAllId.length;i++){
            if(selectAllId[i]==id){
                count+=1;
            }
        }
        if(count==0){
            selectAllId.push(id);
        }
    } else {
        //设置checked属性
        $("input[id=id]").prop("checked",false);
        for(var i=0;i<selectAllId.length;i++){
            if(selectAllId[i]==id){
                selectAllId.splice(i,1);
            }
        }
    }
    if(selectAllId.length==selectObj.length){
        $("input[name='checkboxAll']").prop("checked", true);
    }else{
        $("input[name='checkboxAll']").prop("checked",false);
    }
}

function deleteSelectedAll() {
    if(selectAllId.length==0){
        ShowTip("请选择需要删除的项！", "danger", 1000);
        return;
    }
    var ids='';
    for(var i=0;i<selectAllId.length;i++){
        ids+="'"+selectAllId[i]+"'"+","
    }
    ids=ids.substring(0,ids.length-1);

    Showbox.Msg.confirm('您确定删除所选记录吗？',function(v){
        if(v=='yes'){
            $.ajax({
                type: "delete",
                url: "/ebb/train/member/deleteSelectedAll/",
                contentType: 'application/json',
                data: ids,
                success: function (data, status) {
                    if(data>0){
                        ShowTip("删除成功"+data+"条！", "success", 2000);
                        loadProduct();
                    }else{
                        ShowTip("删除失败！", "danger", 1000);
                    }
                },
                error:function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    ShowTip(XMLHttpRequest, "error", 1000);
                }
            });
        }
        if(v=='no'){
        }
    });
}

function comments() {
  trainTitleName= $("#trainTitleName").val().trim();
  memberName= $("#memberName").val().trim();
  addressCode=$("#addressCode option:selected").val();
 console.log("trainTitleName="+trainTitleName+",memberName="+memberName+"addressCode"+addressCode);
    loadProduct();
}




$('#go-index').click(function () {
    var index = $('#page-index').val();
    if(index>totalSum){
        index = totalSum;
    }
  indexPa=parseInt(index);
    loadProduct();
});



//删掉专产品http://cloud.vimi8.top/industrial/product/?id=old_5606f0798200756ca8cc621d
          function deleteTrainInfo(id){
                 if(confirm("确定要删除该培训记录吗？")){
                  $.ajax({
                   type:"DELETE",
                   url:"/ebb/train/info/"+id,
                    dataType: "json",
                    success: function(data){
                          window.location.reload();
                    }
                });
               }
          }

function fillTrainInfo(selectId){// addressCode
// ebb/geo/?addressCode=520600000000&regionLever=3
    //  value="--选择区域--"
    if(selectId){
        $("#"+selectId).append("<option value=''>--选择培训信息--</option>");;
    }

      $.ajax({
                       type:"GET",
                       url:"/ebb/train/info/all/",
                        dataType: "json",
                        success: function(data){
                            console.log("data="+data);
                            if(data){
                               $.each(data,function(i,item){
                                        console.log("item :" + item);
                                        $("#"+selectId).append("<option value='"+item.id+"'>"+item.trainTitle+"-"+item.trainDate+"</option>");
                                    })
                            }
                        }
                    });
}
